﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sharp.Domain.Repository.EF;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Crosscutting;
using Sharp.Infrastructure.Security;

namespace Sharp.Domain.CMSBoundedContext.AritcleModule.Aggregates
{
    public partial class GatherRepository
    {
        /// <summary>
        /// 获取采集规则
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [Cache]
        public IList<Gather> GetRules(string ids)
        {
            ((EntityFrameworkRepositoryContext) this.Context).Context.Configuration.AutoDetectChangesEnabled = false;
            ((EntityFrameworkRepositoryContext)this.Context).Context.Configuration.LazyLoadingEnabled = false;

            List<Guid?> Ids = new List<Guid?>();
            Ids.AddRange(ids.Split(',').Select(Guid.Parse).Cast<Guid?>());
            var list =
                this.GetList(x => !x.IsDisabled && (Ids.Count == 0 || (Ids.Contains(x.Id) || Ids.Contains(x.ParentId))))
                    .OrderBy(x => x.ParentId).ThenBy(x => x.Id).ToList();

            return list;
        }



        /// <summary>
        /// 最后一次工作时间
        /// </summary>
        /// <param name="id"></param>
        public void LastWork(Guid id)
        {
            using (Entities db = new Entities())
            {
                db.Database.ExecuteSqlCommand("UPDATE Gathers SET LastWork={1} WHERE Id={0}", id, DateTime.Now);
            }
        }
    }
}